/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.pub.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.pub.entity.ScreenPoint;
import com.thinkgem.jeesite.modules.pub.service.ScreenPointService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.pub.entity.AdMake;
import com.thinkgem.jeesite.modules.pub.service.AdMakeService;

import java.util.ArrayList;
import java.util.List;

/**
 * 广告定制Controller
 * @author 开发
 * @version 2016-07-13
 */
@Controller
@RequestMapping(value = "${adminPath}/pub/adMake")
public class AdMakeController extends BaseController {

	@Autowired
	private AdMakeService adMakeService;

	@Autowired
	private ScreenPointService screenPointService;
	
	@ModelAttribute
	public AdMake get(@RequestParam(required=false) String id) {
		AdMake entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = adMakeService.get(id);
		}
		if (entity == null){
			entity = new AdMake();
		}
		return entity;
	}
	
	@RequiresPermissions("pub:adMake:view")
	@RequestMapping(value = {"list", ""})
	public String list(AdMake adMake, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AdMake> page = adMakeService.findPage(new Page<AdMake>(request, response), adMake);
		List<AdMake> list = new ArrayList<AdMake>();
		String[] pointId;
		for(AdMake adMake1 : page.getList()){
			if(adMake1.getPoint().getId().contains(",")){
				pointId = adMake1.getPoint().getId().split(",");
				for (int i = 0; i < pointId.length; i++) {
					ScreenPoint screenPoint = screenPointService.get(pointId[i]);
					if(i ==0){
						adMake1.getPoint().setName(screenPoint.getName());
					}else{
						adMake1.getPoint().setName(adMake1.getPoint().getName() + "," + screenPoint.getName());
					}

				}
			}else{
				adMake1.getPoint().setName(screenPointService.get(adMake1.getPoint().getId()).getName());
			}

		}
		model.addAttribute("page", page);
		return "modules/pub/adMakeList";
	}

	@RequiresPermissions("pub:adMake:view")
	@RequestMapping(value = "form")
	public String form(AdMake adMake, Model model) {
		model.addAttribute("adMake", adMake);
		return "modules/pub/adMakeForm";
	}

	@RequiresPermissions("pub:adMake:edit")
	@RequestMapping(value = "save")
	public String save(AdMake adMake, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, adMake)){
			return form(adMake, model);
		}
		adMakeService.save(adMake);
		addMessage(redirectAttributes, "保存广告成功");
		return "redirect:"+Global.getAdminPath()+"/pub/adMake/?repage";
	}
	
	@RequiresPermissions("pub:adMake:edit")
	@RequestMapping(value = "delete")
	public String delete(AdMake adMake,@RequestParam(required = false) Boolean isRe, RedirectAttributes redirectAttributes) {
        if(!UserUtils.getSubject().isPermitted("pub:adMake:audit")){
            addMessage(redirectAttributes,"您没有删除或发布权限");
        }
		adMakeService.delete(adMake,isRe);
		addMessage(redirectAttributes, (isRe!=null&&isRe?"审核":"删除")+"广告成功");
		return "redirect:"+Global.getAdminPath()+"/pub/adMake/?repage";
	}

}